package com.tianqu.tms.www.tradeStatistics.controller;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import com.tianqu.tms.core.base.Logger;
import com.tianqu.tms.core.base.TmsController;
import com.tianqu.tms.core.base.TxController;
import com.tianqu.tms.core.base.UserContext;
import com.tianqu.tms.core.exception.TmsException;
import com.tianqu.tms.core.exception.TransactionException;
import com.tianqu.tms.core.security.TmsUserProfile;
import com.tianqu.tms.core.util.TmsConstants;
import com.tianqu.tms.entity.Car;
import com.tianqu.tms.entity.Codes;
import com.tianqu.tms.entity.CommercialVehicle;
import com.tianqu.tms.entity.MarketCarTransferInfo;
import com.tianqu.tms.entity.TransferPersonalCarInfo;
import com.tianqu.tms.entity.VehicleBrand;
import com.tianqu.tms.www.tradeStatistics.actionform.SpecialCarForm;
import com.tianqu.tms.www.tradeStatistics.viewhelper.TradeStatisticsViewhelper;

public class SpecialCarNameInputResultController extends TmsController{
	private Logger logger = Logger.getLogger(SpecialCarNameInputResultController.class);
	public final static String NSERIAL = "nSerial";
	@Override
	public void execute(UserContext uc) throws Exception {
		TxController controller = TxController.getTxController();
		// 添加事务提交异常捕获
		try {
			controller.begin();
			SpecialCarForm carForm = (SpecialCarForm)uc.getInputForm();
			TradeStatisticsViewhelper view = new TradeStatisticsViewhelper();
			view = (TradeStatisticsViewhelper)uc.getAttribute(TmsConstants.VIEWHELP);
			Codes code = Codes.getInstance();
			List list = new ArrayList();
			if(view != null){
				if("库存".equals(view.getMarketType())){
					Car car = Car.getInstance();
					car.setSerial(view.getSerial());
					car.setDisplacement(Integer.parseInt(carForm.getDisplacement()));
					car.setGearbox(code.getItemCodeByItemIdAndItemValue(Codes.GEARBOX_TYPE,carForm.getGearbox()));
					car.setVendorId(carForm.getVendorId());
					car.setBrandId(carForm.getBrandId());
					car.setCarStyleId(carForm.getCarStyleId());
					//car.setUpdateUserId(profile.getUniqueId());
					CommercialVehicle commercialVehicle = CommercialVehicle.getInstance();
					VehicleBrand brand = VehicleBrand.getInstance();
					if(carForm.getFromType() == 1){
						list = brand.getAllVehicleBrandInfo(carForm.getVendorId());
					}else{
						list = commercialVehicle.getAllVehicleBrandInfo(carForm.getVendorId());
					}
					
					if (list.size() > 0) {
						for (int i = 0; i < list.size(); i++) {
							Map map = (Map) list.get(i);
							if (map.get("brandId").equals(carForm.getBrandId())) {
								String carName = (String) map.get("brandName");
								car.setCarName(carName);
							}
						}
					}else{
						car.setCarName(" ");
					}
					car.updateCarNameS(car);
				}else if("非库存".equals(view.getMarketType())){
					TransferPersonalCarInfo pCar = TransferPersonalCarInfo.getInstance();
					pCar.setSerial(view.getSerial());
					pCar.setDisplacement(Integer.parseInt(carForm.getDisplacement()));
					pCar.setGearbox(code.getItemCodeByItemIdAndItemValue(Codes.GEARBOX_TYPE,carForm.getGearbox()));
					pCar.setVendorId(carForm.getVendorId());
					pCar.setBrandId(carForm.getBrandId());
					pCar.setCarStyleId(carForm.getCarStyleId());
					//pCar.setUpdateUserId(profile.getUniqueId());
					CommercialVehicle commercialVehicle = CommercialVehicle.getInstance();
					VehicleBrand brand = VehicleBrand.getInstance();
					if(carForm.getFromType() == 1){
						list = brand.getAllVehicleBrandInfo(carForm.getVendorId());
					}else{
						list = commercialVehicle.getAllVehicleBrandInfo(carForm.getVendorId());
					}
					
					if (list.size() > 0) {
						for (int i = 0; i < list.size(); i++) {
							Map map = (Map) list.get(i);
							if (map.get("brandId").equals(carForm.getBrandId())) {
								String carName = (String) map.get("brandName");
								pCar.setCarName(carName);
							}
						}
					}else{
						pCar.setCarName(" ");
					}
					pCar.updateCarNameS(pCar);
				}else if("老数据".equals(view.getMarketType())){
					MarketCarTransferInfo marketCarTranserInfo = new MarketCarTransferInfo();
					marketCarTranserInfo.setSerial(view.getSerial());
					marketCarTranserInfo.setDidisplacement(Integer.parseInt(carForm.getDisplacement()));
					marketCarTranserInfo.setGearbox(code.getItemCodeByItemIdAndItemValue(Codes.GEARBOX_TYPE,carForm.getGearbox()));
					marketCarTranserInfo.setVendorId(carForm.getVendorId());
					marketCarTranserInfo.setBrandId(carForm.getBrandId());
					marketCarTranserInfo.setCarStyleId(carForm.getCarStyleId());

					CommercialVehicle commercialVehicle = CommercialVehicle.getInstance();
					VehicleBrand brand = VehicleBrand.getInstance();
					if(carForm.getFromType() == 1){
						list = brand.getAllVehicleBrandInfo(carForm.getVendorId());
					}else{
						list = commercialVehicle.getAllVehicleBrandInfo(carForm.getVendorId());
					}
					
					if (list.size() > 0) {
						for (int i = 0; i < list.size(); i++) {
							Map map = (Map) list.get(i);
							if (map.get("brandId").equals(carForm.getBrandId())) {
								String carName = (String) map.get("brandName");
								marketCarTranserInfo.setCarName(carName);
							}
						}
					}else{
						marketCarTranserInfo.setCarName(" ");
					}
					marketCarTranserInfo.updateMarketCarTransferInfo(marketCarTranserInfo);
				}
				List list1 = new ArrayList();
				
				list1 = (List)uc.getAttribute("sCarList");
				if(list1.size() > 0){
					list1.remove(view.getSerial());
				}
				if(list1.size()>0){
					uc.setAttribute(NSERIAL, list1.get(0));
					view.setGoFlag(1);
				}else{
					view.setGoFlag(2);
				}
				
			}
			uc.setAttribute(TmsConstants.VIEWHELP, view);
			controller.commit();
		} catch (Exception e) {
			try {
				controller.rollback();
			} catch (TransactionException e1) {
				throw new TmsException(TmsException.DB_TXCRROR, e1);
			}
			// 系统异常
			if (e instanceof TmsException) {
				throw e;
			}
			throw new TmsException(TmsException.DB_TXCRROR, e);
		}

		logger.debug(this.getClass().getName() + "#execute was completed.");
	}

}
